using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewAndong.Model;
using NewAndong.Service;
using Leeder.Business;
using Andong.Model;
using Newtonsoft.Json;
using System.Net.WebSockets;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace NewAndong.Business
{
    /// <summary>
    /// 
    /// </summary>
   public class BoardBLL : BaseBLL<BoardModel, BoardService>
   {

        SignalLampDirectionService _signalLampDirectionService;
        WmsDataBLL wmsDataBLL = new WmsDataBLL();   

        public BoardBLL()
        {
            Service = new BoardService();
            _signalLampDirectionService = new SignalLampDirectionService();
        }

        public BoardModel GetModel(string name)
        {
            var result = Service.Query(p => p.Name == name);
            if (result.IsSuccess)
            {
                return result.ActivedObj.FirstOrDefault();
            }
            else
            {
                LogObj.Error(result.Dsca);
                return default;
            }
        }

        public void SetModel(string name,string projectName)
        {
            var result = Service.Query(p => p.Name == name);
            if (!result.IsSuccess)
            {
                LogObj.Error(result.Dsca);
                return;
            }
            var boardModel = result.ActivedObj.FirstOrDefault();
            if (boardModel != null)
            {
                var statusList = JsonConvert.DeserializeObject<List<AnDongModel>>(boardModel.Value);
                foreach (var item in statusList)
                {
                    if (projectName == item.LampName)
                    {
                        WmsDataModel wmsDataModel = new WmsDataModel();
                        wmsDataModel.ProjectName = projectName;
                        wmsDataModel.LinkNumber = item.LinkCount.ToString();
                        wmsDataBLL.Add(wmsDataModel);
                        if (item.Status == 1 || item.Status == 2)
                        {
                            item.Status = 0;
                            var getOneInput = new SignalLampDirectionModel();
                            getOneInput.LampName = item.LampName;
                            var singal = _signalLampDirectionService.GetOne(getOneInput);
                            if (singal != null)
                            {
                                singal.Status = 0;
                                singal.RealDelayCount = "";
                                singal.DeliveryStatus = 1;
                                _signalLampDirectionService.Update(singal);
                            }
                            break;
                        }

                    }
                }
                //_redisHelper.HashSet("AnDong", input.UnpackingId.ToString(), JsonConvert.SerializeObject(statusList));
                //result.ActivedObj = statusList;

                var model = GetModel(name);
                if (model != null)
                {
                    model.Value = JsonConvert.SerializeObject(statusList);
                    Service.Update(model);
                }
                else
                {
                    //model = new BoardModel();
                    //model.UnpackingId = unpackingId;
                    //model.Value = JsonConvert.SerializeObject(statusList);
                    //Service.Add(model);
                }
            }
        }
    }
}
